package com.gf.common.util;

import javax.annotation.Resource;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gf.common.exception.BusinessException;

/**
 * 短信发送平台<br>
 * 取该实例时用springUtils.getBean("smsHelper");
 * @author zhuguifeng guifeng.net@gmail.com
 * @version 创建时间：2012-10-1 下午2:41:32
 */
@Resource
public class SmsHelper {

	private static String Utf_8_Url;
	private static String Uid;
	private static String Key;
	private static Log logger = LogFactory.getLog(SmsHelper.class);

	/**
	 * -1 没有该用户账户<br>
	 * -2 密钥不正确（不是用户密码）<br>
	 * -3 短信数量不足<br>
	 * -11 该用户被禁用<br>
	 * -14 短信内容出现非法字符<br>
	 * -4 手机号格式不正确<br>
	 * -41 手机号码为空<br>
	 * -42 短信内容为空<br>
	 * 大于0 短信发送数量<br>
	 * 
	 * @param smsMob
	 * @param smsText
	 * @return
	 */
	private static int send(String smsMob, String smsText) {
		int result = 0;
		try {
			HttpClient httpClient = new HttpClient();
			PostMethod postMethod = new PostMethod(Utf_8_Url);
			postMethod.addRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=utf-8");
			NameValuePair[] pairs = { new NameValuePair("Uid", Uid),
					new NameValuePair("Key", Key),
					new NameValuePair("smsMob", smsMob),
					new NameValuePair("smsText", smsText) };
			postMethod.setRequestBody(pairs);
			httpClient.executeMethod(postMethod);

			// 返回状态查询
			// Header[] headers = postMethod.getResponseHeaders();
			int statusCode = postMethod.getStatusCode();
			logger.info("statusCode:" + statusCode);

			// for (Header h : headers) {
			// System.out.println(h.toString());
			// }
			String res = new String(postMethod.getResponseBodyAsString()
					.getBytes("gbk"));
			result = Integer.valueOf(res);
			logger.info("发送结果：result=" + res);
			postMethod.releaseConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 接收者只有一个
	 * 
	 * @param smsMob
	 *            接收者
	 * @param smsText
	 *            短信内容
	 * @return
	 */
	public static int sendSingle(String smsMob, String smsText) {
		return send(smsMob, smsText);
	}

	/**
	 * 接收者只有多个
	 * 
	 * @param smsMobs
	 *            接收者 一次最多对50个手机发送 半角逗号隔开
	 * @param smsText
	 *            短信内容
	 * @return
	 */
	public static int sendMulti(String[] smsMobs, String smsText) {
		if (smsMobs == null || smsMobs.length <= 0) {
			throw new BusinessException("接收者不能为空");
		}
		// 1
		else if (smsMobs.length == 1) {
			return send(smsMobs[0], smsText);
		}
		// 2--50
		else if (1 < smsMobs.length && smsMobs.length <= 50) {
			String result = smsMobs[0];
			for (int i = 1; i < smsMobs.length; i++) {
				result += ("," + smsMobs[i]);
			}
			return send(result, smsText);
		}
		// 超过50条
		else {
			throw new BusinessException("接收者过多");
		}

	}

	/*********** getter setter ****************/
	public String getUtf_8_Url() {
		return Utf_8_Url;
	}

	public void setUtf_8_Url(String utf_8_Url) {
		Utf_8_Url = utf_8_Url;
	}

	public String getUid() {
		return Uid;
	}

	public void setUid(String uid) {
		Uid = uid;
	}

	public String getKey() {
		return Key;
	}

	public void setKey(String key) {
		Key = key;
	}

}
